import { IRequestOption } from '@jl/common/composable';
import { request } from '@jl/common/utils';
import axios from 'axios';
import { Files } from './types';

// 通用文件上传
export const commonUpload = (file: any, onUploadProgress?: any, cancelToken?: any) => {
  const formData = new FormData();
  formData.append('files', file);
  return request({
    url: '/api/BasOpe/ReceiveCommonFile2',
    method: 'POST',
    data: formData,
    onUploadProgress,
    cancelToken,
  });
};

// 大文件上传
export const commonLargeUpload = (param: any, onUploadProgress?: any, cancelToken?: any) => {
  const formData = new FormData();
  formData.append('ResourceId', param.resourceId);
  formData.append('DataFile', param.file);
  return request({
    url: '/api/BasOpe/ReceiveLargeFile',
    method: 'POST',
    timeout: 0,
    data: formData,
    onUploadProgress,
    cancelToken,
  });
};

/**
 * 上传切片
 * @param requestOption - 请求参数
 * @returns
 */
export const upSplice = (param: any, onUploadProgress?: any, cancelToken?: any): Promise<any> => {
  const formData = new FormData();
  formData.append('TransId', param.TransId);
  formData.append('Index', param.Index);
  formData.append('Size', param.Size);
  formData.append('TotalSlice', param.TotalSlice);
  formData.append('SubFile', param.SubFile, `sliceIndex.${param.Index}.jlkj`);

  return request({
    url: '/api/resource/recShareFile',
    method: 'POST',
    timeout: 0,
    data: formData,
    onUploadProgress,
    cancelToken,
  });
};

/**
 * 资源新增 大文件   获取文件上传状态和进度
 * @param requestOption - 请求参数
 * @returns
 */
export const getUploadProg = (transId: any): Promise<any> => {
  return request({
    url: `/api/resource/getUploadProg?transId=${transId}`,
    method: 'GET',
  });
};

/**
 * 资源新增 大文件   获取资源处理状态和进度
 * @param requestOption - 请求参数
 * @returns
 */
export const getResoProg = (resoId: any): Promise<any> => {
  return request({
    url: `/api/resource/getResoProg?resoId=${resoId}`,
    method: 'GET',
  });
};

/**
 * 资源新增 大文件   获取分片状态
 * @param requestOption - 请求参数
 * @returns
 */
export const getSliceState = (transId: any, totalSlice: number): Promise<any> => {
  return request({
    url: `/api/resource/getSliceState?transId=${transId}&totalSlice=${totalSlice}`,
    method: 'GET',
  });
};

/**
 * 资源新增 大文件   合并文件
 * @param requestOption - 请求参数
 * @returns
 */
export const mergerFile = (data: any): Promise<any> => {
  return request({
    url: `/api/resource/mergerFile`,
    method: 'PUT',
    params: data,
  });
};

// 上传base64转图片
export const uploadBase64 = (base64: string, onUploadProgress?: any, cancelToken?: any) => {
  const formData = new FormData();
  formData.append('base64String', base64);
  return request({
    url: '/api/resource/recBase64File',
    method: 'POST',
    data: formData,
    onUploadProgress,
    cancelToken,
  });
};

// 天地图poi搜索
export const tdtSearch = (params: any) => {
  return axios.request({
    url: `http://api.tianditu.gov.cn/v2/search?postStr={"keyWord":"${params.keyWord}","level":12,"mapBound":"-180,-90,180,90","queryType":1,"start":0,"count":20,"show":"1"}&type=query&tk=2d2239f1c7aed662cb4523fed1af8510`,
    method: 'GET',
  });
};

// 天地图逆地理编码查询
export const tdtGeocoderSearch = (params: any) => {
  return axios.request({
    url: `http://api.tianditu.gov.cn/geocoder?postStr={'lon':${params.lon},'lat':${params.lat},'ver':1}&type=geocode&tk=2d2239f1c7aed662cb4523fed1af8510`,
    method: 'GET',
  });
};

// 获取常用文件列表
export const getFileList = (pageInfo: any, params: any): Promise<Files.Result> => {
  const { pageSize, pageNum } = pageInfo;
  return request({
    url: `/api/resource/getCommFileList/${pageSize}/${pageNum}`,
    method: 'GET',
    params,
  });
};

// 通用文件上传
export const uploadFile = (data: any) => {
  return request({
    url: '/api/resource/unifyReceCommFile',
    method: 'POST',
    data,
  });
};

// 删除常用文件
export const deleteFile = (params: any) => {
  return request({
    url: '/api/resource/delCommonFile',
    method: 'DELETE',
    params,
  });
};

/**
 * 查询省市区
 */
export const getRegion = async (data: { parentRegionCode?: number }): Promise<any> => {
  return await request({
    url: '/auth/admin/resource/regionTree',
    method: 'POST',
    data,
  });
};
